﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Back.Xinkui.Entity.Category;
using Back.Xinkui.DataAccess.Common;
using System.Text;

public partial class UserControls_UC_Category : System.Web.UI.UserControl
{
    public int CategoryID
    { get; set; }

    private List<CategoryEntity> _Categorys;
    protected List<CategoryEntity> Categorys
    {
        get
        {
            if (_Categorys == null)
            {
                _Categorys = CategoryDataAccess.GetCategoryList();
            }
            return _Categorys;
        }
    }

    protected StringBuilder TopCategory
    { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        TopCategory = new StringBuilder();
        var tops = Categorys.Where(u => u.ParentID == 0).OrderBy(u => u.OrderBy).ToList();
        var selecttop = tops.FirstOrDefault();
        tops.ForEach(u =>
        {
            TopCategory.AppendLine(string.Format(@"<option value=""{0}"">{1}</option>", u.CategoryID, u.Title));
        });

        if (CategoryID > 0)
        {
            this.ltCategoryName.Text = CategoryView(CategoryID);
        }
    }

    public string CategoryView(int categoryID)
    {
        var selectCategory = Categorys.FirstOrDefault(u => u.CategoryID == categoryID);

        if (selectCategory != null && selectCategory.ParentID > 0)
        {
            return CategoryView(selectCategory.ParentID) + ">>" + selectCategory.Title;
        }
        else
        {
            return selectCategory.Title;
        }
    }



}